<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/6 0006
 * Time: 下午 3:41
 */

namespace Api\Controller;


use Think\Controller;
use Think\Model;

class PublicController extends Controller
{
    /**
     * 发送短信验证码
     */
    public function sendSms()
    {

        vendor('Sms.sms');
        $url = "http://www.ztsms.cn/sendNSms.do";//提交地址
        $username = 'XR2017hy';//用户名
        $password = 'StpOEk';//原密码
        $code = randNum();
        $sendAPI = new \sendAPI($url, $username, $password);
        $data = array(
            'content' => '验证码为' . $code . "【希尔问卷】",//短信内容
            'mobile' => I('post.mobile'),//手机号码
            'productid' => '676767',//产品id
            'xh' => ''//小号
        );
        $sendAPI->data = $data;//初始化数据包
        $return = $sendAPI->sendSMS('POST');//GET or POST
        if ($return == 1) {
            $data['code'] = $code;
            $data['time'] = time();
            $data['state'] = 2;
            M('sms')->add($data);
            $this->ajaxreturn(array('state' => 1, 'data' => $data), 'json');
        } else {
            $this->ajaxreturn(array('state' => 0, 'data' => $return), 'json');
        }
    }

    /**
     * 验证验证码
     */
    public function checkCode($mobile, $code)
    {
        if (empty($mobile)) {
            $mobile = I('request.mobile');
        }
        if (empty($code)) {
            $code = I('request.code');
        }
        if ($mobile && $code) {
            $sql_code = M('sms')->where("mobile='$mobile' and state=2")->order('id desc')->field(array('code', 'state', 'id'))->find();
            $code_id = $sql_code['id'];

            if ($code == $sql_code['code']) {
                M('sms')->where("id='$code_id'")->setField('state', 1);

                $this->ajaxreturn(array('state' => 1, 'data' => 1), 'json');
            } else {
                //验证码错误
                $this->ajaxreturn(array('state' => -1, 'data' => -1), 'json');
            }
        } else {
            //没有手机号或者验证码
            $this->ajaxreturn(array('state' => -2, 'data' => -1), 'json');
        }
    }

    /**
     * 获取轮播
     */
    public function getBanner()
    {
        $result = M('banner')->select();
        if ($result) {
            $this->ajaxreturn(array('state' => 1, 'data' => $result), 'json');
        } else {
            $this->ajaxreturn(array('state' => 0), 'json');
        }
    }

    /**
     * 参加 活动
     */
    public function joinActivity()
    {
        $openid = $this->get_openid();
        $mobile = I('request.mobile');
        $data['openid'] = $openid;
        $goods_id = I('request.goods_id');
        //goods表
        $old_people_result = M('good')->where("id='$goods_id'")->find();
//        $map['openid'] = $openid;
//        $map['goods_id'] = $goods_id;
//        $is_joindata = M('good')->where($map)->find();
//        dump($is_joindata);
//
//        if ($is_joindata){
//            $this->ajaxreturn(array('state' => -3), 'json');die;
//        }
        $group_id = $old_people_result['group_id'];
        //所有人都可以参与
        if ($old_people_result['all_people'] != 1) {
            //有资格参加这个活动的人
            if ($group_id) {
                $group_result = M('group_user')->where("group_id in ($group_id)")->field(array('mobile'))->select();

                if ($group_result) {
                    //获取有资格参加手机号的总条数
                    $count_group_result = count($group_result);
                    for ($i = 0; $i <= $count_group_result; $i++) {
                        //判断当前登录人的手机号  是不是在群组里
                        if ($mobile == $group_result[$i]['mobile']) {
                            $activity = M('activity')->where("goods_id='$goods_id' and openid='$openid'")->find();
                            //如果这个人参加过了,就不增加参与人数了
                            if (empty($activity)) {
                                $newpeople = $old_people_result['people'] + 1;
                                //更新参加活动的人数
                                $result = M('good')->where("id='$goods_id'")->setField('people', $newpeople);
                            }
                            break;
                        } else {
                            if ($i == $count_group_result) {
                                $this->ajaxreturn(array('state' => -2), 'json');
                            }
                        }
                    }
                } else {
                    //没有资格参加活动
                    $this->ajaxreturn(array('state' => -3), 'json');
                }

            } else {
                //没有选群组(基本不可能存在)
                $this->ajaxreturn(array('state' => -4), 'json');
            }
        } else {
            //判断他是不是参加过一次了
            $activity = M('activity')->where("goods_id='$goods_id' and openid='$openid'")->find();
            if (empty($activity)) {
                $newpeople = $old_people_result['people'] + 1;
                //更新参加活动的人数
                $result = M('good')->where("id='$goods_id'")->setField('people', $newpeople);
            }
        }
        $data['goods_id'] = $goods_id;
        $uuid = uuid($openid . $data['goods_id']);
        //获取到地址中#所在的地方  并且插入uuid
        $local = strpos($old_people_result['url'], "#");
        $url = substr_replace($old_people_result['url'], "?uuid=$uuid", $local, 0);
        $data['uuid'] = $uuid;
        $data['time'] = time();
        $act_result = M('activity')->add($data);
        if (empty($old_people_result) || empty($act_result)) {
            $this->ajaxreturn(array('state' => -1), 'json');

        } else {
            $this->ajaxreturn(array('state' => 1, 'data' => $url), 'json');
        }
    }


    /**
     * 答题反馈
     */
    public function answer()
    {
        $this->ajaxreturn($this->answer1());
//        $openid = $this->get_openid();
//        $goods_id = I('request.goods_id');
////        $uuid = M('user')->where("id='$id'")->getField('uuid');
//        $uuid = M('activity')->where("goods_id='$goods_id' and openid='$openid'")->getField('uuid');
////      $url = "http://www.kangpy.com:9093/mobile/webSampleDetail.action?param="."{"."id:"."$uuid"."}";
////        $url = "https://www.hillwx.cn:9093/mobile/webSampleDetail.action?param=" . '{"id":' . '"' . "$uuid" . '"}';
//        $url = "https://www.hillwx.cn/mobile/webSampleDetail.action?param=" . '{"id":' . '"' . "$uuid" . '"}';
////        $url = "http://www.kangpy.com:9093/mobile/webSampleDetail.action?param={%22id%22:%22$uuid%22}";
//        $result = file_get_contents($url);
//        $this->ajaxreturn(json_decode($result, true));
    }

    /**
     * @return mixed答案借口,在活动记录的地方调用,用来修改活动记录表状态
     */
    public function answer1($uuid){
        $openid = $this->get_openid();
        $goods_id = I('request.goods_id');
//        $uuid = M('user')->where("id='$id'")->getField('uuid');
        if(empty($uuid)){
            $uuid = M('activity')->where("goods_id='$goods_id' and openid='$openid'")->getField('uuid');
        }

//      $url = "http://www.kangpy.com:9093/mobile/webSampleDetail.action?param="."{"."id:"."$uuid"."}";
//        $url = "https://www.hillwx.cn:9093/mobile/webSampleDetail.action?param=" . '{"id":' . '"' . "$uuid" . '"}';
        $url = "https://www.hillwx.cn/mobile/webSampleDetail.action?param=" . '{"id":' . '"' . "$uuid" . '"}';
//        $url = "http://www.kangpy.com:9093/mobile/webSampleDetail.action?param={%22id%22:%22$uuid%22}";
        $result = file_get_contents($url);
        return json_decode($result, true);

    }


    /**
     * @param $jscode
     * @return mixed    小程序 微信授权
     */
    public function getOpenid()
    {
        $appid = 'wx885e5bd4813c58c9';
        $appsecret = '7529c92260da2e583cca2355bc28499a';
        $jscode = $_REQUEST['js_code'];
        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$appsecret&js_code=$jscode&grant_type=authorization_code";
        $info = file_get_contents($url);
        $json = json_decode($info);//对json数据解码
        $arr = get_object_vars($json);//对象转数组
        $openid = $arr['openid'];
        $session_key = $arr['session_key'];
        $result = M('user')->where("openid='$openid'")->find();
        if ($result) {
            $this->ajaxreturn($arr['openid'], 'json');
        } else {
            if ($openid) {
                $data['openid'] = $openid;
                $result = M('user')->add($data);
                $this->ajaxreturn($data['openid'], 'json');
            } else {
                $this->ajaxreturn(-1, 'json');
            }

        }

    }

    /**
     * 配置站点名称,
     */
    public function config()
    {
        $result = M('config')->order('id desc')->find();
        if ($result) {
            $this->ajaxreturn(array('state' => 1, 'data' => $result), 'json');
        } else {
            $this->ajaxreturn(array('state' => 0, 'data' => $result), 'json');
        }
    }

    /**
     * 获取用户协议
     */
    public function getAgreement()
    {
        $agreement=M('config')->order("id desc")->getfield("agreement");
        if($agreement){
            $this->ajaxreturn(array('state'=>1,'data'=>$agreement),'json');
        }else{
            $this->ajaxreturn(array('state'=>0),'json');
        }
    }

}